package com.rms.recruit_management_system.controller;

import com.rms.recruit_management_system.pojo.Positions;
import com.rms.recruit_management_system.service.impl.PositionsServiceImpl;
import com.rms.recruit_management_system.utils.Result;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Slf4j
@RestController
public class ExcelImportController {

    @Autowired
    private PositionsServiceImpl positionsService;
    @RequestMapping("/positions/importExcel")
    public Result<?> positionImportExcel(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return new Result<>("请选择一个文件");
        }
        List<Positions> positionsList = new ArrayList<>();
        try (InputStream inputStream = file.getInputStream()) {
            Workbook workbook = null;
            if (file.getOriginalFilename().endsWith(".xls")) {
                workbook = new HSSFWorkbook(inputStream);
            } else if (file.getOriginalFilename().endsWith(".xlsx")) {
                workbook = new XSSFWorkbook(inputStream);
            } else {
                return new Result<>("不支持的文件格式");
            }
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                Positions positions = new Positions();
                Cell cell0 = row.getCell(0);
                if (cell0!= null) {
                    positions.setPositionName(getCellValue(cell0));
                }
                Cell cell1 = row.getCell(1);
                if (cell1!= null) {
                    positions.setPositionDescription(getCellValue(cell1));
                }
                Cell cell2 = row.getCell(2);
                if (cell2!= null) {
                    positions.setDegreeRequirement(getCellValue(cell2));
                }
                Cell cell3=row.getCell(3);
                if(cell3!=null){
                    positions.setWorkExperience(getCellValue(cell3));
                }
                Cell cell4=row.getCell(4);
                if(cell4 !=null){
                    positions.setSalary(getCellValue(cell4));
                }
                Cell cell5=row.getCell(5);
                if(cell5 !=null){
                    positions.setDepartmentName(getCellValue(cell5));
                }
                positions.setUpdateTime(LocalDateTime.now());
                positions.setCreateTime(LocalDateTime.now());
                // 继续添加更多字段
                positionsList.add(positions);
            }
            positionsService.saveBatch(positionsList);
            return new Result<>( 200,"文件导入成功",positionsList);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(400,"文件导入失败");
        }
    }

    @RequestMapping("/positions/exportExcel")
    public Result<?> exportPositionsToExcel(HttpServletResponse response)  {

        positionsService.exportPositionsToExcel(response);
        return new Result<>(200,"文件导出成功");
    }


    private String getCellValue(Cell cell) {
        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                return String.valueOf(cell.getNumericCellValue());
            case BOOLEAN:
                return String.valueOf(cell.getBooleanCellValue());
            default:
                return "";
        }
    }
}
